package com.dianping.cat.home.group.transform;
import com.dianping.cat.home.group.IVisitor;
import com.dianping.cat.home.group.entity.Domain;
import com.dianping.cat.home.group.entity.DomainGroup;
import com.dianping.cat.home.group.entity.Group;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  15 */     this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static DomainGroup parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static DomainGroup parse(InputStream in) {
/*  28 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    DomainGroup domainGroup = new DomainGroup();

    try {
      domainGroup.accept(parser);
/*  33 */     } catch (RuntimeException e) {
/*  34 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  37 */         throw e;
      }
    }

/*  41 */     parser.m_linker.finish();
/*  42 */     return domainGroup;
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  49 */     while ((tag = readTag()) != -1) {
/*  50 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  55 */     switch (_field) {
      case 1:
/*  57 */         domain.setId(readString());
        break;
      case 33:
/*  60 */         if (_type == 1) {
/*  61 */           Group group = new Group();

/*  63 */           visitGroup(group);
/*  64 */           this.m_linker.onGroup(domain, group); break;
/*  65 */         }  if (_type == 2) {
/*  66 */           for (int i = readInt(); i > 0; i--) {
/*  67 */             Group group = new Group();

/*  69 */             visitGroup(group);
/*  70 */             this.m_linker.onGroup(domain, group);
          }
        }
        break;
    }
  }



  public void visitDomainGroup(DomainGroup domainGroup) {
    byte tag;
/*  81 */     if ((tag = readTag()) != -4) {
/*  82 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  85 */     while ((tag = readTag()) != -1) {
/*  86 */       visitDomainGroupChildren(domainGroup, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainGroupChildren(DomainGroup domainGroup, int _field, int _type) {
/*  91 */     switch (_field) {
      case 33:
/*  93 */         if (_type == 1) {
/*  94 */           Domain domain = new Domain();

/*  96 */           visitDomain(domain);
/*  97 */           this.m_linker.onDomain(domainGroup, domain); break;
/*  98 */         }  if (_type == 2) {
/*  99 */           for (int i = readInt(); i > 0; i--) {
/* 100 */             Domain domain = new Domain();

/* 102 */             visitDomain(domain);
/* 103 */             this.m_linker.onDomain(domainGroup, domain);
          }
        }
        break;
    }
  }



  public void visitGroup(Group group) {
    byte tag;
/* 114 */     while ((tag = readTag()) != -1) {
/* 115 */       visitGroupChildren(group, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitGroupChildren(Group group, int _field, int _type) {
/* 120 */     switch (_field) {
      case 1:
/* 122 */         group.setId(readString());
        break;
      case 2:
/* 125 */         if (_type == 1) {
/* 126 */           group.addIp(readString()); break;
/* 127 */         }  if (_type == 2) {
/* 128 */           for (int i = readInt(); i > 0; i--) {
/* 129 */             group.addIp(readString());
          }
        }
        break;
    }
  }

  private int readInt() {
    try {
/* 138 */       return (int)readVarint(32);
/* 139 */     } catch (IOException e) {
/* 140 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 146 */       return this.m_in.readUTF();
/* 147 */     } catch (IOException e) {
/* 148 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 154 */       return this.m_in.readByte();
/* 155 */     } catch (IOException e) {
/* 156 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 161 */     int shift = 0;
/* 162 */     long result = 0L;

/* 164 */     while (shift < length) {
/* 165 */       byte b = this.m_in.readByte();
/* 166 */       result |= (b & 0x7F) << shift;
/* 167 */       if ((b & 0x80) == 0) {
/* 168 */         return result;
      }
/* 170 */       shift += 7;
    }

/* 173 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/group/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */